package com.leaderhr.fishshop.service.product;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.leaderhr.fishshop.dao.product.IProductTypeDao;
import com.leaderhr.fishshop.form.ProductTypeForm;
import com.leaderhr.fishshop.form.Tree;
import com.leaderhr.fishshop.model.product.ProductType;
@Service("ProductTypeService")
public class ProductTypeServiceImpl implements IProductTypeService {
	
	private IProductTypeDao productTypeDao;
	

	public IProductTypeDao getProductTypeDao() {
		return productTypeDao;
	}

	@Inject
	public void setProductTypeDao(IProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	@Override
	public List<ProductTypeForm> treeGrid() {
		List<ProductTypeForm> ptd = new ArrayList<ProductTypeForm>();
		List<ProductType> pt = productTypeDao.listProductType();
		if (pt != null && pt.size() > 0) {
			for (ProductType pts : pt) {
				ProductTypeForm ptds = new ProductTypeForm();
				ptds.setIconCls("icon-blank");
				BeanUtils.copyProperties(pts, ptds);
				ProductType pty = pts.getParent();// 获得当前节点的父节点对象
				if (pty != null) {
					ptds.setIconCls("icon-blank");
					ptds.setPid(pty.getId());
					ptds.setPname(pty.getName());
				}
				ptd.add(ptds);
			}
		}
		return ptd;
	}

	@Override
	public void add(ProductTypeForm productTypeForm) {
		ProductType productType = new ProductType();
		BeanUtils.copyProperties(productTypeForm, productType);
		if (productTypeForm.getPid() != null && !productTypeForm.getPid().equals("")) {
			productType.setParent(productTypeDao.load(productTypeForm.getPid()));
		}
		productTypeDao.add(productType);
	}

	@Override
	public List<Tree> tree() {
		List<Tree> lt = new ArrayList<Tree>();
		List<ProductType> l = productTypeDao.listProductType();
		if (l != null && l.size() > 0) {
			for (ProductType t : l) {
				Tree tree = new Tree();
				BeanUtils.copyProperties(t, tree);
				tree.setText(t.getName());
				//tree.setIconCls("status_online");
				ProductType pty = t.getParent();// 获得当前节点的父节点对象
				if (pty!= null) {
					tree.setPid(pty.getId());
				}
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public ProductTypeForm load(Integer id) {
		ProductTypeForm r = new ProductTypeForm();
		ProductType t = productTypeDao.load(id);
		if (t != null) {
			BeanUtils.copyProperties(t, r);
			if (t.getParent() != null) {
				r.setPid(t.getParent().getId());
				r.setPname(t.getParent().getName());
			}
		}
		return r;
	}

	@Override
	public void edit(ProductTypeForm productTypeForm) {
		System.out.println(productTypeForm.getId());
		//productTypeDao.update(productTypeForm);
		ProductType t = productTypeDao.load(productTypeForm.getId());
		if (t != null) {
			BeanUtils.copyProperties(productTypeForm, t);
			if (productTypeForm.getPid() != null && !productTypeForm.getPid().equals("")) {
				t.setParent(productTypeDao.load(productTypeForm.getId()));
			}
			if (productTypeForm.getPid() != null && !productTypeForm.getPid().equals("")) {// 说明前台选中了上级资源
				ProductType pt = productTypeDao.load(productTypeForm.getId());
				isChildren(t, pt);// 说明要将当前资源修改到当前资源的子/孙子资源下
				t.setParent(pt);
			} else {
				t.setParent(null);// 前台没有选中上级资源，所以就置空
			}
		}
		
	}
	/**
	 * 判断是否是将当前节点修改到当前节点的子节点
	 * 
	 * @param t
	 *            当前节点
	 * @param pt
	 *            要修改到的节点
	 * @return
	 */
	private boolean isChildren(ProductType t, ProductType pt) {
		if (pt != null && pt.getParent() != null) {
			if (pt.getParent().getId().equals(t.getId())) {
				pt.setParent(null);
				return true;
			} else {
				return isChildren(t, pt.getParent());
			}
		}
		return false;
	}



}
